package cn.leedsoft.and.crimeintent.controller.hellomoon;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
import cn.leedsoft.and.crimeintent.R;

/**
 * Created by leeds on 2016/8/9 22:04.
 */
public class VideoFragment extends Fragment{
//    private VideoPlayer mVideoPlayer = new VideoPlayer();
//    private SurfaceView mSurfaceView;
    private Button showButton;
    private Button hideButton;
    private VideoView vv;
    private MediaController mediaController;
    private int currentPosition = 0;

    public static VideoFragment newInstance() {

        Bundle args = new Bundle();

        VideoFragment fragment = new VideoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.hellomoon_fragment_video, container, false);
        showButton = (Button) view.findViewById(R.id.hellomoon_showbtn);
        hideButton = (Button) view.findViewById(R.id.hellomoon_hidebtn);
        vv = (VideoView) view.findViewById(R.id.hellomoon_video_view);
        mediaController = new MediaController(getActivity());
        vv.setMediaController(mediaController);

        vv.setVideoURI(Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.apollo_17_stroll_1));
        vv.requestFocus();
        showButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaController.show(0);
            }
        });
        hideButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaController.hide();
            }
        });

        if(currentPosition != 0) {
            vv.seekTo(currentPosition);
        }
        vv.start();
//        mediaController.show(0);

        vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.start();
            }
        });

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        currentPosition = vv.getCurrentPosition();
    }
}
